package com.buba.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.buba.pojo.system.Health;
import com.buba.system.mapper.HealthMapper;
import com.buba.system.service.HealthService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.buba.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 健康百科表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-06-09
 */
@Service
public class HealthServiceImpl extends ServiceImpl<HealthMapper, Health> implements HealthService {

    /**
     * @Author 梁玉杰
     * @Description //所有健康百科列表
     * @param Health health
     * @Return com.buba.utils.R
     * @throws
     * @Date 2022/6/10 16:00
     **/
    @Override
    public R findAllHealth(Health health) {
        QueryWrapper<Health> queryWrapper = new QueryWrapper<Health>();
        if(!StringUtils.isEmpty(health.getHTitle())){
            queryWrapper.like("h_title", health.getHTitle());
            System.out.println("标题：："+health.getHTitle());
        }
        queryWrapper.orderByDesc("create_time");
        List<Health> h_title = this.list(queryWrapper);
        return R.ok().data("list", h_title);
    }

    @Autowired
    private RedisTemplate redisTemplate;


    /**
     * @Author 梁玉杰
     * @Description //健康百科详情+redis 使用String的 INCR 实现访问量
     * @param Integer id
     * @Return com.buba.utils.R
     * @throws
     * @Date 2022/6/10 16:06
     **/
    @Override
    public R findByHealthID(Integer id) {
        Health health = baseMapper.selectById(id);
        redisTemplate.opsForValue().set("jiankangbaike:"+id,health.getNum()); //先把值设置进去
        redisTemplate.opsForValue().increment("jiankangbaike:"+id);//然后在执行INCR命令+1
        //然后修改数据库的访问量
        health.setNum((Integer) redisTemplate.opsForValue().get("jiankangbaike:"+id));
        baseMapper.updateById(health);
        return R.ok().data("health", health);
    }
}
